<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $components = array(
		'DebugKit.Toolbar',
		'Session',
		'Tool',
		'Auth' => array(
			'loginAction' => '/login',
			'authError' => 'Bạn cần phải đăng nhập để tiếp tục.',
			'flash' => array(
				'element' => 'default',
				'key' => 'auth',
				'params' => array('class' => 'message')
			),
			'loginRedirect' => '/',
			'logoutRedirect' => DOMAIN_ROOT
			//'logoutRedirect' => array('controller' => 'news', 'action' => 'admin_index')
			
		),
		'RequestHandler'
	);
	
	
	public function beforeFilter() {
	//pr($this->layout);
		$this->set('logged_in', $this->get_user());
		if(substr($this->request->params['action'],0 , 5) == 'admin'){
			$user = $this->get_user();
			if(!$user || $user['group_id'] != 1){
				 $this->redirect('/login');
			}else {
				$this->layout = 'backend';
			}
		}else {
			$this->Auth->allow($this->request->params['action']);
			$this->layout = 'frontend';
		}
			// pr($this->check_group());
		if ($this->RequestHandler->isMobile()) {
			
			$this->layout = 'mobile';
			$this->is_mobile = true;
			$this->set('is_mobile', true );
			$this->autoRender = false;
		}	
		
	}
	
	public function getDevice(){

		if(preg_match('/Iphone/i', $_SERVER['HTTP_USER_AGENT'])) {
			$os = 2;
			echo 'iphone';
		}
		elseif(preg_match('/Android/i', $_SERVER['HTTP_USER_AGENT'])) {
			$os = 1;
			echo 'android';
		}
		elseif(preg_match('/Windows Phone/i', $_SERVER['HTTP_USER_AGENT'])) {
			$os = 3;
		}else {
			$os = '';
		}
		
	return $os;	
	}
	public function afterFilter() {
		if (isset($this->is_mobile) && $this->is_mobile) {
			$this->layout = 'mobile';
			$this->render('/mobile/' . $this->request->params['controller'] . '/' . $this->action);
		
		}
	  }
	
	public function get_alias($name) {
		$alias = $this->Tool->slug($name);
		return $alias;
	}
	
	public function uploadFile($folder, $model, $name) {
		$file = new File($this->request->data[$model][$name]['tmp_name']);
		$arr = explode('.', $this->request->data[$model][$name]['name']);
		$ext = end($arr);
		$filename = $this->request->data[$model]['alias'] . '.' . $ext;
		
		if($file->copy($folder . $filename)){
			$result = array(
				'status' => true,
				'filename' => $filename
			);
		}else {
			$result = array(
				'status' => false
			);
		}
		return $result;
	}
	
	public function get_user() {
		if($this->Auth->login()) {
			$user_login = $this->Auth->user();
		}else {
			$user_login = '';
		}
		return $user_login;
	}
}
